package com.tracy.lifeproject.discovery.goods.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.bean.BuyerRecordBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.discovery.goods.adapter.BuyerRecordAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ZJS on 2016/7/24.
 */
public class BuyerRecordActivity extends Activity {
    @InjectView(R.id.img_buyRecordBack)
    ImageView imgBuyRecordBack;
    @InjectView(R.id.txt_buyRecordCount)
    TextView txtBuyRecordCount;
    @InjectView(R.id.recycler_buyerRecord)
    RecyclerView recyclerBuyerRecord;
    private String url;
    private List<BuyerRecordBean.DataBean.FavoriteListsBean> mDatas;
    private BuyerRecordAdapter buyerRecordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buyerrecord);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        if (id != -1) {
            url = String.format(UrlString.URL_GOODS_BUYERRECORD, id);
            mDatas = new ArrayList<>();
            loadData(url);
            buyerRecordAdapter = new BuyerRecordAdapter(this, mDatas);
            recyclerBuyerRecord.setAdapter(buyerRecordAdapter);
            recyclerBuyerRecord.setLayoutManager(new LinearLayoutManager(this));
        }
    }

    private void loadData(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(BuyerRecordActivity.this, "清单收录加载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                BuyerRecordBean buyerRecordBean = new Gson().fromJson(result, BuyerRecordBean.class);
                mDatas.clear();
                mDatas.addAll(buyerRecordBean.getData().getFavorite_lists());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txtBuyRecordCount.setText(mDatas.size()+"个清单收录");
                        buyerRecordAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    @OnClick(R.id.img_buyRecordBack)
    public void onClick() {
        finish();
    }
}
